
package com.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


/**
 * 这段 Java 代码定义了一个名为 SpringContextUtils 的工具类，
 * 用于在 Spring 应用程序中获取 ApplicationContext
 * 并提供一些便捷方法来操作该上下文。
 */
@Component//定义为 Spring Bean
public class SpringContextUtils implements ApplicationContextAware {
	public static ApplicationContext applicationContext; 
	 //实现 ApplicationContextAware 接口的方法
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringContextUtils.applicationContext = applicationContext;
	}

	public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}
	//获取 Bean
	public static <T> T getBean(String name, Class<T> requiredType) {
		return applicationContext.getBean(name, requiredType);
	}
//判断是否包含 Bean
	public static boolean containsBean(String name) {
		return applicationContext.containsBean(name);
	}
//判断是否为单例 Bean
	public static boolean isSingleton(String name) {
		return applicationContext.isSingleton(name);
	}
//
	public static Class<? extends Object> getType(String name) {
		return applicationContext.getType(name);
	}
//获取 Bean 名称列表
}